package com.ctg.itrdc.imageconsole.data.config.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.ctg.itrdc.imageconsole.data.config.dao.IImageConfigDao;
import com.ctg.itrdc.imageconsole.data.entity.ImageConfig;
import com.ctg.itrdc.imageconsole.data.entity.ImageConfigName;
import com.ctg.itrdc.imageconsole.data.entity.ImageConfigSet;
import com.ctg.itrdc.imageconsole.data.entity.ImageConfigVersion;
import com.ctg.itrdc.paspsdk.common.entity.PageInfo;
import com.ctg.itrdc.paspsdk.data.dao.impl.BaseDaoImpl;
import com.ctg.itrdc.paspsdk.data.entity.ClassInfoUtils;

@Component("imageConfigDao")
public class ImageConfigDaoImpl extends BaseDaoImpl implements IImageConfigDao {

	@Override
	public PageInfo<ImageConfig> jdbcFindPageInfo(Long dockerImageTagId, int currentPage, int perPageNum) {

		StringBuffer sql = new StringBuffer(" SELECT ");
		String tableName = " " + ClassInfoUtils.getTableName(ImageConfig.class) + " ";
		String columnsName = ClassInfoUtils.getColumnsName(ImageConfig.class);
		sql.append(columnsName).append(" from ").append(tableName).append(" WHERE 1=1")
				.append(" AND docker_image_tag_id = ?");
		List<Object> params = new ArrayList<Object>();
		params.add(dockerImageTagId);

		return (PageInfo<ImageConfig>) this.jdbcFindPageInfo(sql.toString(), ImageConfig.class, params, currentPage,
				perPageNum, true);
	}

	@Override
	public void addImageConfig(ImageConfigSet image) {
		this.insert(image);

	}

	@Override
	public List<ImageConfig> checkImageConfigVersion(Long dockerImageTagId, String configVersion,String configName) {
		String tableName = ClassInfoUtils.getTableName(ImageConfig.class);
		String columnsName = ClassInfoUtils.getColumnsName(ImageConfig.class);
		StringBuilder sql = new StringBuilder(" SELECT ");
		sql.append(columnsName).append(" FROM ").append(tableName).append(" WHERE 1=1 ");

		List<Object> params = new ArrayList<Object>();
		if (null != dockerImageTagId && null != configVersion && null!=configName) {
			sql.append("AND DOCKER_IMAGE_TAG_ID =? ");
			params.add(dockerImageTagId);
			sql.append("AND CONFIG_VERSION =? ");
			params.add(configVersion);
			sql.append("AND CONFIG_NAME =? ");
			params.add(configName);
		}
		// this.getJdbcTemplate().afterPropertiesSet();

		List<ImageConfig> list = this.jdbcFindList(sql.toString(), ImageConfig.class, params, false);

		return list;
	}

	@Override
	public List<ImageConfigName> queryImageConfigNameByImageId(Long dockerImageTagId) {
		String tableName = ClassInfoUtils.getTableName(ImageConfigName.class);
		String columnsName = "config_name,min(id),docker_image_tag_id";
		StringBuilder sql = new StringBuilder(" SELECT ");
		sql.append(columnsName).append(" FROM ").append(tableName).append(" WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();

		if (null != dockerImageTagId) {
			sql.append("AND DOCKER_IMAGE_TAG_ID =? ");
			params.add(dockerImageTagId);

		}

		sql.append("group by config_name");
		return this.jdbcFindList(sql.toString(), ImageConfigName.class, params, false);
	}

	@Override
	public List<ImageConfigVersion> queryImageConfigVersion(Long dockerImageTagId, String configName) {
		String tableName = ClassInfoUtils.getTableName(ImageConfigVersion.class);
		String columnsName = ClassInfoUtils.getColumnsName(ImageConfigVersion.class);

		StringBuilder sql = new StringBuilder(" SELECT ");
		sql.append(columnsName).append(" FROM ").append(tableName).append(" WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();

		if (null != dockerImageTagId && null != configName) {
			sql.append("AND DOCKER_IMAGE_TAG_ID =? ");
			params.add(dockerImageTagId);
			sql.append("AND CONFIG_NAME =? ");
			params.add(configName);
		}
		return this.jdbcFindList(sql.toString(), ImageConfigVersion.class, params, false);
	}

	@Override
	public List<ImageConfig> queryImageConfigByVersion(Long dockerImageTagId, String configName,
			String config_version) {

		String tableName = ClassInfoUtils.getTableName(ImageConfig.class);
		String columnsName = ClassInfoUtils.getColumnsName(ImageConfig.class);

		StringBuilder sql = new StringBuilder(" SELECT ");
		sql.append(columnsName).append(" FROM ").append(tableName).append(" WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();

		if (null != dockerImageTagId && null != configName) {
			sql.append("AND DOCKER_IMAGE_TAG_ID =? ");
			params.add(dockerImageTagId);
			sql.append("AND CONFIG_NAME =? ");
			params.add(configName);
			sql.append("AND CONFIG_VERSION =? ");
			params.add(config_version);
		}
		return this.jdbcFindList(sql.toString(), ImageConfig.class, params, false);
	}

	@Override
	public PageInfo<ImageConfig> jdbcFindPageInfo(String configName, Long dockerImageTagId, int pageNum, int pageSize) {
		StringBuffer sql = new StringBuffer(" SELECT ");
		String tableName = " " + ClassInfoUtils.getTableName(ImageConfig.class) + " ";
		String columnsName = ClassInfoUtils.getColumnsName(ImageConfig.class);
		sql.append(columnsName).append(" from ").append(tableName).append(" WHERE 1=1");
		List<Object> params = new ArrayList<Object>();
		if (null != configName) {
			sql.append(" AND CONFIG_NAME = ?");
			params.add(configName);

		}
		if (null != dockerImageTagId) {
			sql.append(" AND DOCKER_IMAGE_TAG_ID = ?");
			params.add(dockerImageTagId);
		}

		return (PageInfo<ImageConfig>) this.jdbcFindPageInfo(sql.toString(), ImageConfig.class, params, pageNum,
				pageSize, true);
	}

}
